import { statementListApi } from '@/api/pcs/pcsStatementManage'
import { defaultPageSize } from '@/utils/antdHelper'
import { formatDate } from '@/utils/displayHelper'
import modelExtend from '@/utils/dvaUtil/modelExtend'
import { PCS_STATEMENT_STATUS_ENUM } from '@/utils/enum'

export const namespace = 'pcsStatementManage'

const initialState = {
  searchParams: {
    status: PCS_STATEMENT_STATUS_ENUM.BE_PAID,
  },
  pagination: {
    current: 1,
    pageSize: defaultPageSize,
    total: 0,
  },
  pageList: [],
}

export default modelExtend({
  namespace,
  state: initialState,
  matchedActions: toAction => {
    toAction('getPageList')
  },
  effects: {
    *getPageList({ payload }, { call, update, select }) {
      const { searchParams, pagination } = yield select(state => state[namespace])
      const params = {
        ...searchParams,
        ...pagination,
        ...payload,
      }

      if (params.serviceDate?.length) {
        params.serviceStartDate = formatDate(params.serviceDate[0])
        params.serviceEndDate = formatDate(params.serviceDate[1])
        delete params.serviceDate
      }

      if (params.confirmDate?.length) {
        params.confirmStartDate = formatDate(params.confirmDate[0])
        params.confirmEndDate = formatDate(params.confirmDate[1])
        delete params.confirmDate
      }

      if (params.payDate?.length) {
        params.payStartDate = formatDate(params.payDate[0])
        params.payEndDate = formatDate(params.payDate[1])
        delete params.payDate
      }

      const { data } = yield call(statementListApi, params)
      const { content, current, pageSize, total, totalNursingAmount } = data
      yield update({ pageList: content, pagination: { current, pageSize, total }, totalNursingAmount })
    },
  },
})
